import Vue from 'vue'
import Router from 'vue-router'
import bookCity from '@/view/bookCity'
import bookShelf from '@/view/bookShelf'
import detail from '@/view/detail'
import ready from '@/view/ready'

Vue.use(Router)

let router =  new Router({
  routes: [
    {
        path: '/',
        redirect: {name:'bookCity'}
    },
    {
        path:'/bookCity',
        name:'bookCity',
        component:bookCity
    },
    {
        path:'/bookShelf',
        name:'bookShelf',
        component:bookShelf
    },
    {
        path:'/detail/:id',
        name:'detail',
        component:detail
    },
    {
        path:'/login',
        name:'login',
        component:login
    },
    {
        path:'/ready/:id',
        name:'ready',
        component:ready,
        // beforeEnter(to,from,next){  //路由独享守卫
        //     if(localStorage.getItem('isLogin')){
        //         next()
        //     }else{
        //         next('/login')
        //     }
        // }
    }
  ]
})

let pathname = ['play'];
router.beforeEach((to,from,next) => {
    if(pathname.indexOf(to.name) != -1){
        if(localStorage.getItem('isLogin')){
            next();
        }else{
            next('/login');
        }
    }else{
        next();
    }
})


export default router


//守卫：组件内守卫  路由独享守卫  全局守卫

//this.$router.push()       添加一条记录

//this.$router.replace()    替换当前记录

//传参：params  query

//全局路由对象$router   当前路由对象：$route

//redirect    name

